﻿using System;
using BLL;
using Microsoft.VisualBasic;
using System.Drawing;
using System.Threading;

public partial class InstrumentManage_ViewENAState : System.Web.UI.Page
{
    Microsoft.VisualBasic.Devices.Computer com = new Microsoft.VisualBasic.Devices.Computer();

    //是查看在线状态还是工作状态
    private bool IsPing
    {
        get { return bool.Parse(ViewState["IsPing"].ToString()); }
        set { ViewState["IsPing"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            IsPing = false;
            BindENA();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        IsPing = true;
        PingENA();
    }

    protected void BindENA()
    {
        ENA eNA = new ENA();
        this.GridView1.DataSource = eNA.GetENAs();
        this.GridView1.DataBind();
    }

    //无参数线程模拟
    //protected void Ping1()
    //{
    //    bool result = com.Network.Ping(this.GridView1.Rows[1].Cells[2].Text.Trim());
    //    if (result == true)
    //    {
    //        //this.Button1.BackColor = Color.Green;
    //        this.GridView1.Rows[1].Cells[4].Text = "在线";
    //        this.GridView1.Rows[1].Cells[4].BackColor = Color.Green;
    //    }
    //    else
    //    {
    //        //this.Button1.BackColor = Color.Red;
    //        this.GridView1.Rows[1].Cells[4].Text = "不在线";
    //        this.GridView1.Rows[1].Cells[4].BackColor = Color.Red;
    //    }
    //}

    protected void Ping(object o)
    {
        int i = Convert.ToInt32(o);
        try
        {
            bool result = com.Network.Ping(this.GridView1.Rows[i].Cells[2].Text.Trim());
            if (result == true)
            {
                //this.Button1.BackColor = Color.Green;
                this.GridView1.Rows[i].Cells[5].Text = "在线";
                this.GridView1.Rows[i].Cells[5].BackColor = Color.Green;
            }
            else
            {
                //this.Button1.BackColor = Color.Red;
                this.GridView1.Rows[i].Cells[5].Text = "不在线";
                this.GridView1.Rows[i].Cells[5].BackColor = Color.Red;
            }
        }
        catch
        {
            return;
        }
    }

    protected void PingENA()
    {
        int i, n = GridView1.Rows.Count;
        Thread[] thr = new Thread[n];
        for (i = 0; i < n; i++)
        {
            thr[i] = new Thread(new ParameterizedThreadStart(Ping));
            thr[i].Start(i);
        }
        for (int j = 0; j < n; j++)
        {
            thr[j].Join();
        }

        //多地址单线程
        //int i;
        //for (i = 0; i < this.GridView1.Rows.Count; i++)
        //{
        //    bool result = com.Network.Ping(this.GridView1.Rows[i].Cells[2].Text.Trim());
        //    if (result == true)
        //    {
        //        //this.Button1.BackColor = Color.Green;
        //        this.GridView1.Rows[i].Cells[4].Text = "在线";
        //        this.GridView1.Rows[i].Cells[4].BackColor = Color.Green;
        //    }
        //    else
        //    {
        //        //this.Button1.BackColor = Color.Red;
        //        this.GridView1.Rows[i].Cells[4].Text = "不在线";
        //        this.GridView1.Rows[i].Cells[4].BackColor = Color.Red;
        //    }
        //}

        //简单的单地址测试
        //bool result = com.Network.Ping(TextBox1.Text);
        //if (result == true)
        //    this.Button1.BackColor = Color.Green;
        //else
        //    this.Button1.BackColor = Color.Red;
    }

    protected void GridView1_PageIndexChanging(object sender, System.Web.UI.WebControls.GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        BindENA();
        if (IsPing)
        {
            PingENA();
        }
    }
}
